package com.ruoyi.aicall.controller;


import com.ruoyi.aicall.domain.CcCallPhone;
import com.ruoyi.aicall.domain.CcCallTask;
import com.ruoyi.aicall.model.*;
import com.ruoyi.aicall.service.ICcCallPhoneService;
import com.ruoyi.aicall.service.ICcCallTaskService;
import com.ruoyi.cc.service.ICcParamsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.UuidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/aicall/api")
public class ApiController {

    @Autowired
    private ICcCallTaskService callTaskService;
    @Autowired
    private ICcCallPhoneService ccCallPhoneService;
    @Autowired
    private ICcParamsService paramsService;

    @PostMapping("/notice/call")
    @ResponseBody
    public AjaxResult callNotice(@RequestBody NoticeCallModel noticeCallModel) {
        // 获取任务
        String batchName = paramsService.getParamValueByCode("testNoticeCallTaskName", "test");
        String phoneNum = paramsService.getParamValueByCode("testNoticeCallPhoneNum", "13908113506");
        CcCallTask ccCallTask = callTaskService.selectCcCallTaskByBatchName(batchName, 2);

        // 追加名单
        CcCallPhone callPhone = buildCcCallPhone(ccCallTask.getBatchId(), phoneNum);
        callPhone.setTtsText(noticeCallModel.getNoticeContent());
        ccCallPhoneService.insertCcCallPhone(callPhone);

        // 启动任务
        ccCallTask.setIfcall(1);
        ccCallTask.setExecuting(0L);
        callTaskService.updateCcCallTask(ccCallTask);

        return AjaxResult.success();
    }


    @PostMapping("/ai/addCallList")
    @ResponseBody
    public AjaxResult addAiCallList(@RequestBody AiCallListModel aiCallListModel) {
        // 获取任务
        CcCallTask ccCallTask = callTaskService.selectCcCallTaskByBatchId(aiCallListModel.getTaskId());

        // 追加名单
        List<CcCallPhone> callPhoneList = new ArrayList<>();
        for (String phoneNum: aiCallListModel.getPhoneList()) {
            CcCallPhone callPhone = buildCcCallPhone(ccCallTask.getBatchId(), phoneNum);
            callPhoneList.add(callPhone);
            if (callPhoneList.size() >= 200) {
                ccCallPhoneService.batchInsertCcCallPhone(callPhoneList);
                callPhoneList = new ArrayList<>();
            }
        }
        if (callPhoneList.size() > 0) {
            ccCallPhoneService.batchInsertCcCallPhone(callPhoneList);
        }

        // 启动任务
        ccCallTask.setIfcall(1);
        ccCallTask.setExecuting(0L);
        callTaskService.updateCcCallTask(ccCallTask);

        return AjaxResult.success();
    }



    @PostMapping("/notice/addCallList")
    @ResponseBody
    public AjaxResult addNoticeCallList(@RequestBody NoticeCallListModel noticeCallListModel) {
        // 获取任务
        CcCallTask ccCallTask = callTaskService.selectCcCallTaskByBatchId(noticeCallListModel.getTaskId());

        // 追加名单
        List<CcCallPhone> callPhoneList = new ArrayList<>();
        for (NoticePhoneModel noticePhoneModel: noticeCallListModel.getPhoneList()) {
            String phoneNum = noticePhoneModel.getPhoneNum();
            CcCallPhone callPhone = buildCcCallPhone(ccCallTask.getBatchId(), phoneNum);
            callPhone.setTtsText(noticePhoneModel.getNoticeContent());
            callPhoneList.add(callPhone);
            if (callPhoneList.size() >= 200) {
                ccCallPhoneService.batchInsertCcCallPhone(callPhoneList);
                callPhoneList = new ArrayList<>();
            }
        }
        if (callPhoneList.size() > 0) {
            ccCallPhoneService.batchInsertCcCallPhone(callPhoneList);
        }

        // 启动任务
        ccCallTask.setIfcall(1);
        ccCallTask.setExecuting(0L);
        callTaskService.updateCcCallTask(ccCallTask);

        return AjaxResult.success();
    }

    private CcCallPhone buildCcCallPhone(Long batchId, String phoneNum) {
        CcCallPhone callPhone = new CcCallPhone();
        callPhone.setId(UuidGenerator.GetOneUuid());
        callPhone.setGroupId("1");
        callPhone.setBatchId(batchId);
        callPhone.setCreatetime(new Date().getTime());
        callPhone.setCallstatus(0);
        callPhone.setCalloutTime(0L);
        callPhone.setCallcount(0);
        callPhone.setCallEndTime(0L);
        callPhone.setTimeLen(0L);
        callPhone.setValidTimeLen(0L);
        callPhone.setUuid("");
        callPhone.setConnectedTime(0L);
        callPhone.setHangupCause("");
        callPhone.setAnsweredTime(0L);
        callPhone.setDialogue("");
        callPhone.setWavfile("");
        callPhone.setRecordServerUrl("");
        callPhone.setBizJson("");
        callPhone.setDialogueCount(0L);
        callPhone.setAcdOpnum("");
        callPhone.setAcdQueueTime(0L);
        callPhone.setAcdWaitTime(0);
        callPhone.setTelephone(phoneNum);
        callPhone.setCustName("");
        return callPhone;
    }
}
